package com.sunwoda.springbootredis.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.sun.deploy.net.HttpResponse;
import com.sunwoda.springbootredis.dto.UserDto;
import com.sunwoda.springbootredis.entity.ResultEntity;
import com.sunwoda.springbootredis.entity.User;
import com.sunwoda.springbootredis.result.Result;
import com.sunwoda.springbootredis.service.UserService;
import com.sunwoda.springbootredis.util.ExcelUtil;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.poi.ss.usermodel.Workbook;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-09-11 08:45:49
 */
@RestController
@RequestMapping("user")
public class UserController {

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ResultEntity selectOne(Long id) {
        return Result.customSuccess(this.userService.queryById(id));
    }

    /**
     * @description: 获取用户列表
     * @param:
     * @return:
     * @author shiguorang
     * @date: 2021/9/11 9:35
     */
    @PostMapping("/getList")
    public ResultEntity getList(@RequestBody User re){
        List<User> list = this.userService.getList(re);
        return Result.customSuccess(list);
    }
    /**
     * @description: 删除key
     * @param:
     * @return:
     * @author shiguorang
     * @date: 2021/9/11 9:46
     */
    @GetMapping("opsKey")
    public void opsKey(){
        this.userService.opsKey();
    }

    /**
     * @description: null
     * @param:
     * @return:
     * @author shiguorang
     * @date: 2021/9/12 11:24
     */
    @GetMapping(value = "/bound")
    public void bound(){
        this.userService.bound();
    }

    /**
    * @Description: 获取所有数据
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-15
    */
    @GetMapping(value = "getAll")
    public void getAll(){
        this.userService.getAll();
    }

    /**
     * @description: 修改
     * @param:
     * @return:
     * @author shiguorang
     * @date: 2021/9/22 21:21
     */
    @PutMapping(value = "update")
    public ResultEntity update(@RequestBody User re){
        int update = this.userService.update(re);
        return Result.customSuccess(update);
    }

    /**
     * @description: 通过实体类查询单个用户
     * @param:
     * @return:
     * @author shiguorang
     * @date: 2021/9/22 22:05
     */
    @RequestMapping(value = "/queryOne")
    public ResultEntity queryOne(@RequestBody User re){
        User user = this.userService.queryOne(re);
        return Result.customSuccess(user);
    }


    /**
    * @Description: 导出
    * @Param:
    * @return:
    * @Author: SGR
    * @Date: 2021-09-23
    */
    @PostMapping(value = "/export")
    public void export(@RequestBody User re, HttpServletResponse response) throws IOException {
        List<User> list = this.userService.getList(re);
        List<UserDto> userDtos = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        list.forEach(one -> {
            UserDto dto = new UserDto();
            BeanUtils.copyProperties(one,dto);
            String care = dateFormat.format(one.getCareatAt());
            dto.setCareatAt(care);
            String update = dateFormat.format(one.getUpdateAt());
            dto.setUpdateAt(update);
            userDtos.add(dto);
        });

        BeanUtils.copyProperties(list,userDtos);
        ExcelUtil.exportExcel(userDtos,"用户列表","用户列表",UserDto.class,"用户列表.xls", response);
    }

}